/**
 * 
 */
package net.sapfree.core.web;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;


/**
 * @author QiuZhiLei
 *
 */

public class HTMLGenerator {
	
	private ParamProcessor m_pp = new ParamProcessor();

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{

	}
	
    /**
     *���http���󴮹���һ��form �����е�'&'��0x07���棬Ҳ������'\&';'='��0x08���棬Ҳ������'\='
     * @param formName
     * @param formId
     * @param action
     * @return
     */
    public static String getHiddenFormHtml(String formName,String formId,String action){
    	
    	String parameterstring = "";
    	try{
    		int splitIndex = action.indexOf('?');
    		if(splitIndex>0){
    			parameterstring = action.substring(splitIndex+1);
        		action = action.substring(0, splitIndex);
    		}else{
    		}
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    	
    	return getHiddenFormHtml(formName,formId,action,parameterstring);

    }
    /**
     *���http���󴮹���һ��form �����е�'&'��0x07���棬Ҳ������'\&';'='��0x08���棬Ҳ������'\='
     * @param formName
     * @param formId
     * @param action
     * @param parameterstring
     * @return
     */
    public static String getHiddenFormHtml(String formName,String formId,String action,String parameterstring){
    	
    	StringBuffer form = new StringBuffer("<form method=\"post\" ");
    	if(formName!=null && !"".equals(formName))
    		form.append("name=\"").append(formName).append("\" ");
    	if(formId!=null && !"".equals(formId))
    		form.append("id=\"").append(formId).append("\" ");
    	if(action!=null && !"".equals(action))
    		form.append("action=\"").append(action).append("\" ");
    	form.append(">");
    	try{
    		parameterstring = parameterstring.replace("\\&", "\007").replace("\\=", "\010");
	    	String[] parameters = parameterstring.split("&");
	    	String name  = null;
	    	String value = null;
	    	int index=0;
			for( index = 0; index < parameters.length; index++)
			{
				String[] name_value = parameters[index].split("=");
			    name = name_value[0];
			    value = name_value.length==1 ? "" : name_value[1];
			    if(value!=null){
			    	form.append("<input type=\"hidden\" name=\"").append(name).append("\" value=\"").append(value.replace('\007','&').replace('\010','=').replace("\"","&quot;")).append("\">");
			    	form.append("\n");
			    }
			}
			 
    	}catch(Exception e){
    		e.printStackTrace();
    	}

    	form.append("</form>");
    	return form.toString();
    }
    
    
    /**
     * ���select��ѡ�����һ��select�?��������option��value��text��ͬ���������map��key��Ӧ��option��value
     * @param head select��ʼ��ǩ��<select >���������,eg:<select name=s id=s >����headΪname=s id=s
     * @param option ��ѡ�����ʹ��LinkedMap
     * @param defautvalue  ��select�ʼʱĬ��ѡ�е�ֵ
     * @return
     */

	public static String getSelectElementHtml(String head, Map option, String defautvalue){

		StringBuffer sb = new StringBuffer();
		sb.append("<select ").append(head).append(" >");
		if(option != null)
		{
			Set keys = option.keySet();
			Iterator it = keys.iterator();
			while( it.hasNext() )
			{
				String key = (String)it.next();
				if(key == null ) continue;
				String value = (String)option.get(key);
				sb.append("<option value=\"").append(key).append("\"");
				if(key.equals(defautvalue))
					 sb.append(" selected=\"selected\"");
				sb.append(">").append(value).append("</option>");
			}
		}
		sb.append("</select>");
		return sb.toString();
	}
	
    /**
     * ���select��ѡ�����һ��select�?��������option��value��text��ͬ�����
     * @param head select��ʼ��ǩ��<select >���������,eg:<select name=s id=s >����headΪname=s id=s
     * @param option ��ѡ��
     * @param defautvalue  ��select�ʼʱĬ��ѡ�е�ֵ
     * @return
     */
	public static String getSelectElementHtml(String head, List option, String defautvalue){

		StringBuffer sb = new StringBuffer();
		sb.append("<select ").append(head).append(" >");
		if(option != null)
		{
			for(int i=0;i<option.size();i++){
				String value = (String)option.get(i);
				if(value == null ) continue;
				sb.append("<option value=\"").append(value).append("\"");
				if(value.equals(defautvalue))
					 sb.append(" selected=\"selected\"");
				sb.append(">").append(value).append("</option>");
			}
		}
		
		sb.append("</select>");
		return sb.toString();
	}
	
	/**
	 * 
	 * @param prop
	 * @param items
	 * @param selectedValues
	 * @return
	 */
	public static String getCheckBoxGroupHtml(String prop,Map items,List selectedValues,int startIndex, int endIndex)
	{	
		Set keys = items.keySet();
		Iterator it = keys.iterator();
		StringBuffer sb = new StringBuffer();
		while( it.hasNext() )
		{
			String key = (String)it.next();
			String value = (String)items.get(key);
			sb.append("<input type=\"checkbox\" ").append(prop).append(" ");
			sb.append(" value=\"").append(key).append("\" ");
			if(selectedValues!=null && selectedValues.contains(key))
			{
				 sb.append(" checked=\"checked\" ");
			}
			sb.append(" />").append(value).append("&nbsp;");
		}
		return sb.toString();
	}
	
	public static String getCheckBoxGroupHtml(String prop,Map items,String selectedValues,int startIndex, int endIndex)
	{	
		Set keys = items.keySet();
		Iterator it = keys.iterator();
		StringBuffer sb = new StringBuffer();
		while( it.hasNext() )
		{
			String key = (String)it.next();
			String value = (String)items.get(key);
			sb.append("<input type=\"checkbox\" ").append(prop).append(" ");
			sb.append(" value=\"").append(key).append("\" ");
			if(selectedValues!=null && selectedValues.contains(key))
			{
				 sb.append(" checked=\"checked\" ");
			}
			sb.append(" />").append(value).append("&nbsp;");
		}
		return sb.toString();
	}
	
	public static String getCheckBoxGroupHtml(String prop,Map items,String selectedValues)
	{
		return getCheckBoxGroupHtml(prop,items,selectedValues,0, 0);
	}
}
